#pragma once

#ifndef WEBWND_H
#define WEBWND_H

#include <QtCore>
#include <QtGui>
#include <QtWidgets>
#include "ExtendWidget.h"

#include "webview.h"

namespace Ui {
class WebWnd;
}

class WebWnd : public ExtendWidget
{
    Q_OBJECT
public:
    explicit WebWnd(const QString & strUrl = "", QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    ~WebWnd();

public:
    WebView * getView() { return m_view; }
    QLineEdit * getUrlWidget();

protected:
    virtual void closeEvent(QCloseEvent *event) override;
    virtual void firstShowedEvent(QEvent * event) override;
    virtual QSize sizeHint() const override;

public:
    void back();
    void forward();
    void reload();
    void stop();

    void load(const QUrl & url);
    void loadUrl(const QUrl & url);
    void setUrl(const QString & val);
    QString getUrl();

    void setBrowserFocus(bool focus = true);
    void setNaviFocus(bool focus = true);

protected:
    WebView *   m_view;
    bool        m_bStop;
    QString     m_strUrl;

protected:
    QIcon m_defaultIcon;
    QIcon m_errorIcon;
    QIcon m_stopIcon;
    QIcon m_reloadIcon;
    QIcon m_loadingIcon1;
    QIcon m_loadingIcon2;
    QIcon m_loadingIcon3;
    QIcon m_loadingIcon4;

protected:
    void fini();
    void init();
    void initWeb();

    void updateReloadButton(int progress);

    bool checkRepeatTask();
    void showDownTips();
    void showDownTask();

protected slots:
    void onPageCreated();

    void onLoadUrl();

    void onAddressChanged(QString url);
    void onFaviconChanged(QString strImageUrl, int http_status_code, QIcon icon);
    void onLoadStarted(int transition_type);
    void onLoadingProgressChanged(double progress);
    void onLoadEnded(int httpStatusCode);

    void onWebLinkHovered(QString url);
    void onBack();
    void onForward();
    void onReload();
    void onHome();

signals:
    //////////////////////////////////////////////////////////////////////////
    //
    void loadIconChange(QIcon icon);
    void titleChange(QString title);

    void newWebWnd(QString strUrl, bool current);
    void newMainWnd(QString strUrl);

private:
    Ui::WebWnd *ui;
};

#endif // WEBWND_H
